/**
 * This software was produced for the U. S. Government
 * under Contract No. FA8721-09-C-0002, and is
 * subject to the Rights in Noncommercial Computer Software
 * and Noncommercial Computer Software Documentation Clause
 * (DFARS) 252.227-7014 (JUN 1995)
 * 
 * (c) 2009 The MITRE Corporation. All Rights Reserved.
 *
 * SPMIF to OPNET Translator
 * Author: EWALTERS
 * Created: 9:13:54 AM, Jul 31, 2007
 * MITRE Corporation
 */
package com.mitre.spmif2opnet.analysis.ast;

import java.util.Collection;
import java.util.Hashtable;

import com.mitre.spmif2opnet.analysis.AnalysisException;
import com.mitre.spmif2opnet.analysis.Identifier;

/**
 *  This roughly corresponds to an S-PMIF "project", with additional information 
 */
public class Project {
	
	/*
	 *  Intermediate analysis information
	 */
	private String _network_name;
	/** 
	 * Maintain a map of project/scenario names keyed by file name, 
	 *  in case extra projects need to be pulled in by Link nodes, etc.
	 */
	private Hashtable<String, String>  _file_to_scenario;	
	private Hashtable<String, Scenario> _scenarios;
	/**
	 * What is the project overhead?
	 */
	private Overhead _overhead;
		
	/** 
	 * Default constructor 
	 * @param name project name
	 * @param overhead overhead structure
	 */
	public Project(String name, Overhead overhead)  {		
		_network_name = name;
		_scenarios = new Hashtable<String, Scenario>();		
		_overhead = overhead;
		_file_to_scenario = new Hashtable<String, String>();
	}
	
	/**
	 * Add a scenario to the project
	 * @param s the scenario
	 */
	public void addScenario(Scenario s)	{
		_scenarios.put(s.getName(), s);
		_file_to_scenario.put(s.getFileName(), s.getName());
	}

	/** 
	 * Network name accessor
	 * @return the name
	 */
	public String getName() {
		return _network_name;
	}
		
	/**
	 * Get all of the scenarios
	 * @return the scenarios
	 */
	public Collection<Scenario> getScenarios() {
		return _scenarios.values();
	}
	
	/**
	 * Get a specific scenario by name
	 * @param scen_name the name
	 * @return the scenario
	 * @throws AnalysisException
	 */
	public Scenario getScenario(String scen_name) throws AnalysisException {
		Scenario s = _scenarios.get(scen_name);
		if (s == null) {
			throw new AnalysisException("Attempt to access non-existent scenario " 
					+ scen_name + " from project " + _network_name);
		}
		return s;
	}
	
	/**
	 * Get a specific scenario by id
	 * @param id the id
	 * @return the scenario
	 * @throws AnalysisException
	 */
	public Scenario getScenario(Identifier id) throws AnalysisException {
		if (!id.getProjectName().equals(_network_name)) {
			throw new AnalysisException("Attempt to access project " 
				+ _network_name + " with incorrect identifier " 
				+ id.toString());
		}
		return this.getScenario(id.getScenarioName());
	}
	
	/**
	 * Get a task/graph from this project
	 * @param id the node identifier
	 * @return the task
	 * @throws AnalysisException
	 */
	public Graph getTask(Identifier id) throws AnalysisException {
		Scenario s = this.getScenario(id);
		return s.getTask(id);
	}
	
	/**
	 * Get a node from this project
	 * @param id the node identifier
	 * @return the node
	 * @throws AnalysisException
	 */
	public Node getNode(Identifier id) throws AnalysisException {
		Graph t = this.getTask(id);
		return t.getNode(id);
	}
	
	/**
	 * Return the overhead structure
	 * @return the overhead
	 */
	public Overhead getOverhead() {
		return _overhead;
	}
	
	/**
	 * Get the scenario name by file
	 * @param file_name the file name
	 * @return the scenario name
	 */
	public String scenFromFile(String file_name) {
		return _file_to_scenario.get(file_name);
	}

}
